/*
 * Licensed this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.boapi.shop;

/**
 * International Standard Book Number [ISBN] before 01.01.2007.
 * @author Sebastian Ritter
 * @version 1.0.0.0  
 */
public final class ISBN10 extends ISBN {

  /**
   * Is the checknumber correct
   * @return true, if checknumber is correct 
   */
  public boolean isChecknumberCorrect () {
    int[] factors = new int[] { 10,9,8, 7, 6, 5, 4, 3, 2 }; 
    int[] digits = new int[factors.length];
    for (int i = 0; i < digits.length; i++) {
      digits[i] = Integer.parseInt(this.getNumber().substring (i, i+1));
    }
    int sum = 0;
    for (int i = 0; i < digits.length; i++) {
      sum += digits[i] * factors[i];
    }
    int mod11 = sum % 11;
    int checkDigit = 11 - mod11;
    switch (checkDigit) {
      case 10:
        this.setChecknumber('X');
        break;
      case 11:
        this.setChecknumber('0');
        break;
      default:
        this.setChecknumber((""+checkDigit).charAt(0));
        break;
    }
    return this.getChecknumber() == this.getNumber().toCharArray ()[9];
  }
}
